package com.googlecode.yoohoo.xep.data.protocol;

import junit.framework.Assert;

import org.junit.Test;

import com.googlecode.yoohoo.xep.data.protocol.Data;
import com.googlecode.yoohoo.xep.data.protocol.DataType;
import com.googlecode.yoohoo.xep.data.protocol.Field;
import com.googlecode.yoohoo.xep.data.protocol.FieldType;
import com.googlecode.yoohoo.xep.data.protocol.Item;
import com.googlecode.yoohoo.xep.data.protocol.Option;
import com.googlecode.yoohoo.xep.data.protocol.Reported;

public class DataTest {
	@Test
	public void toXml() {
		String dataXml =
			"<x xmlns='jabber:x:data' type='result'>" +
				"<title>" +
					"Joogle Search: verona" +
				"</title>" +
				"<reported>" +
					"<field var='name'/><field var='url'/>" +
				"</reported>" +
				"<item>" +
					"<field var='name'><value>Comune di Verona - Benvenuti nel sito ufficiale</value></field>" +
					"<field var='url'><value>http://www.comune.verona.it/</value></field>" +
				"</item>" +
				"<item>" + 
					"<field var='name'><value>benvenuto!</value></field>" +
					"<field var='url'><value>http://www.hellasverona.it/</value></field>" +
				"</item>" +
				"<item>" +
					"<field var='name'><value>Universita degli Studi di Verona - Home Page</value></field>" +
					"<field var='url'><value>http://www.univr.it/</value></field>" +
				"</item>" +
			"</x>";
		Data data = new Data();
		data.setType(DataType.RESULT);
		data.setTitle("Joogle Search: verona");
		
		Reported reported = new Reported();
		Field reportedField1 = new Field();
		reportedField1.setVar("name");
		reported.getFields().add(reportedField1);
		
		Field reportedField2 = new Field();
		reportedField2.setVar("url");
		reported.getFields().add(reportedField2);
		data.setReported(reported);
		
		Item item1 = new Item();
		Field item1Field1 = new Field();
		item1Field1.setVar("name");
		item1Field1.getValues().add("Comune di Verona - Benvenuti nel sito ufficiale");
		Field item1Field2 = new Field();
		item1Field2.setVar("url");
		item1Field2.getValues().add("http://www.comune.verona.it/");
		item1.getFields().add(item1Field1);
		item1.getFields().add(item1Field2);
		data.getItems().add(item1);
		
		Item item2 = new Item();
		Field item2Field1 = new Field();
		item2Field1.setVar("name");
		item2Field1.getValues().add("benvenuto!");
		Field item2Field2 = new Field();
		item2Field2.setVar("url");
		item2Field2.getValues().add("http://www.hellasverona.it/");
		item2.getFields().add(item2Field1);
		item2.getFields().add(item2Field2);
		data.getItems().add(item2);
		
		Item item3 = new Item();
		Field item3Field1 = new Field();
		item3Field1.setVar("name");
		item3Field1.getValues().add("Universita degli Studi di Verona - Home Page");
		Field item3Field2 = new Field();
		item3Field2.setVar("url");
		item3Field2.getValues().add("http://www.univr.it/");
		item3.getFields().add(item3Field1);
		item3.getFields().add(item3Field2);
		data.getItems().add(item3);
	
		Assert.assertEquals(dataXml, data.toXml());
		
		dataXml =
			"<x xmlns='jabber:x:data' type='form'>" +
				"<title>Bot Configuration</title>" +
				"<instructions>Fill out this form to configure your new bot!</instructions>" +
				"<field type='hidden' var='FORM_TYPE'>" +
					"<value>jabber:bot</value>" +
				"</field>" +
				"<field type='fixed'><value>Section 1: Bot Info</value></field>" +
				"<field type='boolean' label='Public bot?' var='public'>" +
					"<required/>" +
				"</field>" +
				"<field type='list-multi' label='What features will the bot support?' var='features'>" +
					"<option label='Contests'><value>contests</value></option>" +
					"<option label='News'><value>news</value></option>" +
					"<option label='Polls'><value>polls</value></option>" +
					"<value>news</value>" +
					"<value>search</value>" +
				"</field>" +
			"</x>";
		data = new Data();
		data.setType(DataType.FORM);
		data.setTitle("Bot Configuration");
		data.getInstructions().add("Fill out this form to configure your new bot!");
		
		Field field1 = new Field();
		field1.setVar("FORM_TYPE");
		field1.setType(FieldType.HIDDEN);
		field1.getValues().add("jabber:bot");
		data.getFields().add(field1);
		
		Field field2 = new Field();
		field2.setType(FieldType.FIXED);
		field2.getValues().add("Section 1: Bot Info");
		data.getFields().add(field2);
		
		Field field3 = new Field();
		field3.setVar("public");
		field3.setType(FieldType.BOOLEAN);
		field3.setLabel("Public bot?");
		field3.setRequired(true);
		data.getFields().add(field3);
		
		Field field4 = new Field();
		field4.setVar("features");
		field4.setType(FieldType.LIST_MULTI);
		field4.setLabel("What features will the bot support?");
		Option option1 = new Option("Contests", "contests");
		field4.getOptions().add(option1);
		Option option2 = new Option("News", "news");
		field4.getOptions().add(option2);
		Option option3 = new Option("Polls", "polls");
		field4.getOptions().add(option3);
		field4.getValues().add("news");
		field4.getValues().add("search");
		data.getFields().add(field4);

		Assert.assertEquals(dataXml, data.toXml());
	}
}
